package com.gouwudai.table.tag;

import javax.servlet.jsp.JspException;

import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
import org.extremecomponents.table.bean.Column;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.tag.ColumnTag;

/**
 * <TITLE>LinkColumnTag - 超级链接输出</TITLE>
 * @author <a href="mailto:littcai@hotmail.com">空心大白菜</a>
 * @date 2006-12-1
 * @version 1.0
 *
 */
public class LinkColumnTag extends ColumnTag {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7978155182983598554L;
	private String url;	//URL
	/**
	 * 新页面类型：_self - 标准链接，本页面跳转.
	 * 			 _blank - 标准链接，新页面打开.
	 * 			 _top - 标准链接，顶页面跳转.
	 *           pop - 弹出页面，window.open
	 *           modalDialog - 弹出页面,showModalDialog
	 */
	private String type = "_blank"; 
	private int windowWidth;
	private int windowHeight;
	private int windowLeft;
	private int windowTop;
	private String arguments ; //其他参数，如地址栏、状态栏等
						 


	
	@Override
	public void addColumnAttributes(TableModel tableModel, Column column) {		
		column.addAttribute("url", this.url);
		column.addAttribute("type", this.type);
		column.addAttribute("windowWidth", this.windowWidth);
		column.addAttribute("windowHeight", this.windowHeight);
		column.addAttribute("windowLeft", this.windowLeft);
		column.addAttribute("windowTop", this.windowTop);
		column.addAttribute("arguments", this.arguments);		
	}
	
	

	@Override
	public void modifyColumnAttributes(TableModel tableModel, Column column) {
		column.addAttribute("url", this.url);
		column.addAttribute("type", this.type);
		column.addAttribute("windowWidth", this.windowWidth);
		column.addAttribute("windowHeight", this.windowHeight);
		column.addAttribute("windowLeft", this.windowLeft);
		column.addAttribute("windowTop", this.windowTop);
		column.addAttribute("arguments", this.arguments);
	}

	@Override
	protected Object getColumnValue(Object propertyValue) throws JspException {		
		//如果标签体中有内容，优先处理内容,并返回BODY		
		if(bodyContent!=null)
		{			
			String body = ExpressionEvaluatorManager.evaluate("result", getBodyContent().getString(), Object.class, this, pageContext).toString();
			return body;
		}
		else
		{
			try
			{
			StringBuilder ret = new StringBuilder(100);
			ret.append("<a href=\"");
			ret.append(url);
			ret.append("\"");
			if(type.indexOf("_")>=0)
			{
				ret.append(" target=\"");
				ret.append(type);
				ret.append("\"");
			}
			else if(type.equals("pop"))
			{
				ret.append(" onclick=\"javascript:window.open('");
				ret.append(url);
				ret.append("','','width=");
				ret.append(windowWidth);
				ret.append(",height=");
				ret.append(windowHeight);
				ret.append(",top=");
				ret.append(windowTop);
				ret.append(",left=");
				ret.append(windowLeft);
				if(arguments!=null)
					ret.append(arguments);				
				ret.append("'); return false;\"");				
			}
			else if(type.equals("modalDialog"))
			{
				ret.append(" onclick=\"javascript:showModalDialog('");
				ret.append(url);
				ret.append("','','dialogWidth:");
				ret.append(windowWidth);
				ret.append("px,dialogHeight:");
				ret.append(windowHeight);
				ret.append("px,dialogTop:");
				ret.append(windowTop);
				ret.append("px,dialogLeft:");
				ret.append(windowLeft);
				ret.append("px");
				if(arguments!=null)
					ret.append(arguments);				
				ret.append("'); return false;\"");	
			}
			ret.append(">"+propertyValue+"</a>");
			return ret.toString();
			}
			catch(Exception e)
			{
				return "<font color=\"red\">程序出错，请联系管理员</font>";
			}
		}
		
	}	

	@Override
	public void release() {		
		super.release();
		this.url = null;
		this.type = null;	
		this.arguments = null;
	}



	public String getArguments() {
		return arguments;
	}



	public void setArguments(String arguments) {
		this.arguments = arguments;
	}



	public String getType() {
		return type;
	}



	public void setType(String type) {
		this.type = type;
	}



	public String getUrl() {
		return url;
	}



	public void setUrl(String url) {
		this.url = url;
	}



	public int getWindowHeight() {
		return windowHeight;
	}



	public void setWindowHeight(int windowHeight) {
		this.windowHeight = windowHeight;
	}



	public int getWindowWidth() {
		return windowWidth;
	}



	public void setWindowWidth(int windowWidth) {
		this.windowWidth = windowWidth;
	}



	public int getWindowLeft() {
		return windowLeft;
	}



	public void setWindowLeft(int windowLeft) {
		this.windowLeft = windowLeft;
	}



	public int getWindowTop() {
		return windowTop;
	}



	public void setWindowTop(int windowTop) {
		this.windowTop = windowTop;
	}	



}
